
package com.kugou.game.sdk.ui.widget;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.kugou.game.sdk.utils.AppUtil;
import com.kugou.game.sdk.utils.Rx;

public class HelpMenuPopupWindow extends KGPopupWindow {

    private Context mContext;

    private int mItemCount;

    public static final int HELP_MENU_ITEM_COUNT_TWO = 2;

    public static final int HELP_MENU_ITEM_COUNT_THREE = 3;

    public static final int HELP_MENU_ITEM_COUNT_FOUR = 4;

    private int[] mItemIcons;

    private HelpMenuItemAdapter mHelpMenuItemAdapter;

    /**
     * 可自定义菜单列表item数量和形式的构造方法
     * 
     * @param context
     * @param adapter 自定义item的适配器
     * @param listener item点击的回调监听
     */
    public HelpMenuPopupWindow(Context context, BaseAdapter adapter, OnItemClickListener listener) {
        super(context);
        initList(context, adapter, listener);
    }

    /**
     * 已定义好默认格式的菜单列表
     * 
     * @param context
     * @param count
     * @param listener
     */
    public HelpMenuPopupWindow(Context context, int count, OnItemClickListener listener) {
        super(context);
        mContext = context;
        if (count < 0 || count > 4)
            throw new IllegalArgumentException("please give the right num");
        mItemCount = count;
        mItemIcons = new int[] {

                Rx.drawable.kg_helpmenu_phoneicon,

                Rx.drawable.kg_helpmenu_qqicon,

                Rx.drawable.kg_helpmenu_faq,

                Rx.drawable.kg_helpmenu_rechargetips

        };
        mHelpMenuItemAdapter = new HelpMenuItemAdapter();
        initList(context, mHelpMenuItemAdapter, listener);
    }

    public void hideRechargeTipsItem() {
        if (mItemIcons.length < 3) {
            return;
        }
        mItemCount = 3;
        mHelpMenuItemAdapter.notifyDataSetChanged();
    }

    public void showRechargeTipsItem() {
        if (mItemIcons.length < 4) {
            return;
        }
        mItemCount = 4;
        mHelpMenuItemAdapter.notifyDataSetChanged();
    }

    private void initList(Context context, BaseAdapter adapter, OnItemClickListener listener) {
        View view = LayoutInflater.from(mContext).inflate(Rx.layout.kg_helpmenu_popupwindow, null);
        ListView list = (ListView) view.findViewById(Rx.id.kg_lv_helpmenu);
        list.setAdapter(adapter);
        list.setOnItemClickListener(listener);
        list.setBackgroundResource(context.getResources().getColor(Rx.color.kg_common_transparent));
        setContentView(view);
    }

    @Override
    protected void populateActionItems(ArrayList<ActionItem> actionItems) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onMeasureAndLayout(Rect anchorRect, View contentView) {
        setWidgetSpecs(anchorRect.bottom - AppUtil.dip2px(mContext, 6), false);
        setWidgetSpecsX(anchorRect.left - AppUtil.dip2px(mContext, 62));

    }

    @Override
    protected void showArrow() {
        // TODO Auto-generated method stub

    }

    @Override
    protected void prepareAnimationStyle() {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onShow() {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onDismiss() {
        // TODO Auto-generated method stub

    }

    class HelpMenuItemAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mItemCount;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            if (convertView == null) {
                holder = new Holder();
                convertView = LayoutInflater.from(mContext).inflate(Rx.layout.kg_helpmenu_item,
                        null);
                holder.name = (TextView) convertView.findViewById(Rx.id.kg_tv_menuname);
                holder.icon = (ImageView) convertView.findViewById(Rx.id.kg_iv_helpmenu_icon);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            if (position == 0)
                holder.name.setText("客服电话");
            else if (position == 1)
                holder.name.setText("客服QQ");
            else if (position == 2)
                holder.name.setText("FAQ");
            else
                holder.name.setText("充值提示");

            holder.icon.setBackgroundResource(mItemIcons[position]);
            return convertView;
        }
    }

    class Holder {
        TextView name;

        ImageView icon;
    }

}
